package com;

import java.util.ArrayList;

import com.sun.java_cup.internal.internal_error;

public class HexagonalBoard {
	private ArrayList<Hexagon> cells;
	
	public HexagonalBoard(int size) {
		cells = new ArrayList<Hexagon>();
		this.initializeCells(size);
		this.calculateLineUps(size);
		
		for(int k=0; k<cells.size();k++) {
			Hexagon hexagon = cells.get(k);
			System.out.println(hexagon.getPosition());
			int[] neighbours = hexagon.getNeighbours();
			for(int a=0;a<neighbours.length;a++) {
				System.out.print(neighbours[a]);
			}
			System.out.println("");
		}
	}
	
	private void initializeCells(int s) {
		int topRowCount = (s+1)/2;
		int current = 1;
		for(int i=topRowCount;i<=s;i++) {
			for(int j=1; j<=i; j++) {
				int[] neighbours;
				if(current ==1 ) {
					neighbours = new int[3];
					neighbours[0] = current + 1;
					neighbours[1] = current + topRowCount;
					neighbours[2] = current + topRowCount + 1;
				}
				else if(current == topRowCount) {
					neighbours = new int[3];
					neighbours[0] = current -1;
					neighbours[1] = current + i;
					neighbours[2] = neighbours[1] + 1;
				}
				else if(j==1 && i==s) {
					neighbours = new int[3];
					neighbours[0] = current - s + 1;
					neighbours[1] = current + 1;
					neighbours[2] = current + s;
				}
				else if(j==i && i==s) {
					neighbours = new int[3];
					neighbours[0] = current - s;
					neighbours[1] = current - 1;
					neighbours[2] = current + s - 1;
				}
				else if(j==1) {
					neighbours = new int[4];
					neighbours[0] = current - i + 1;
					neighbours[1] = current + 1;
					neighbours[2] = current + i;
					neighbours[3] = current + i + 1;
				}
				else if(j==i) {
					neighbours = new int[4];
					neighbours[0] = current - i;
					neighbours[1] = current - 1;
					neighbours[2] = current + i;
					neighbours[3] = neighbours[2] + 1;
				}
				else if(current < topRowCount) {
					neighbours = new int[4];
					neighbours[0] = current - 1;
					neighbours[1] = current + 1;
					neighbours[2] = current + i;
					neighbours[3] = neighbours[2] + 1;
				}
				else {
					neighbours = new int[6];
					neighbours[0] = current - i;
					neighbours[1] = neighbours[0] + 1;
					neighbours[2] = current - 1;
					neighbours[3] = current + i;
					neighbours[4] = neighbours[3] + 1;
					neighbours[5] = current + 1;
					if(i==s) {
						neighbours[3] = current + i -1;
						neighbours[4] = current + i;
					}
				}
				Hexagon hexagon = new Hexagon(current, neighbours);
				cells.add(hexagon);
				current++;
			}
		}
		
		for(int k=s-1; k>=topRowCount;k--) {
			for(int m=1;m<=k;m++) {
				int[] neighbours;
				if(k==topRowCount && m==1) {
					neighbours = new int[3];
					neighbours[0] = current + 1;
					neighbours[1] = current - topRowCount;
					neighbours[2] = current - topRowCount - 1;
				}
				else if(k==topRowCount && m==topRowCount) {
					neighbours = new int[3];
					neighbours[0] = current -1;
					neighbours[1] = current - k;
					neighbours[2] = neighbours[1] - 1;
				}
				else if(m==1) {
					neighbours = new int[4];
					neighbours[0] = current - k - 1;
					neighbours[1] = current + 1;
					neighbours[2] = current - k;
					neighbours[3] = current + k;
				}
				else if(m==k) {
					neighbours = new int[4];
					neighbours[0] = current - k;
					neighbours[1] = current - k - 1;
					neighbours[2] = current - 1;
					neighbours[3] = current + k - 1;
				}
				else if(k == topRowCount) {
					neighbours = new int[4];
					neighbours[0] = current - 1;
					neighbours[1] = current + 1;
					neighbours[2] = current - k;
					neighbours[3] = neighbours[2] - 1;
				}
				else {
					neighbours = new int[6];
					neighbours[0] = current - k;
					neighbours[1] = neighbours[0] - 1;
					neighbours[2] = current - 1;
					neighbours[3] = current + k;
					neighbours[4] = neighbours[3] - 1;
					neighbours[5] = current + 1;
				}
				Hexagon hexagon = new Hexagon(current, neighbours);
				cells.add(hexagon);
				current++;
			}
		}
	}
	
	private void calculateLineUps(int s) {
		int[] middleLine = new int[s];//horizontal line
		int[] leftSlope = new int[s];//left slope line
		int[] rightSlope = new int[s];//right slope line
		int topRowCount = (s+1)/2; //number of hexagon in the top row
		
		//calculate left slope line
		leftSlope[0] = 1;
		int i = 1;
		for(;i<topRowCount;i++) {
			leftSlope[i] = leftSlope[i-1] + (topRowCount + i);
		}
		for(int j=topRowCount-1;j>0;j--) {			
			leftSlope[i] = leftSlope[i-1] + (topRowCount + j);
			i++;
		}
		
		//calculate right slope line
		i = 0;
		rightSlope[0] = topRowCount;
		for(;i<topRowCount-1;i++) {
			rightSlope[i+1] = rightSlope[i] + (topRowCount + i);
		}
		
		for(int j=topRowCount-1;j>0;j--) {
			i++;
			rightSlope[i] = rightSlope[i-1] + (topRowCount + j-1);			
		}
		
		//calculate horizontal line
		middleLine[0] = (leftSlope[s-1]+1)/2 - ((topRowCount+1)/2);
		for(int k=1;k<s;k++) {
			middleLine[k]=middleLine[k-1] + 1;
		}
		
/*		for(int k=0; k<s;k++) {
			System.out.println(middleLine[k]);
		}*/
		
	}

}
